Skip to content

Refactor information.py with lots of changes that makes the overall code cleaner and faster#741

Merged
sco1 merged 16 commits into
python-discord:masterfrom
Denayder:information-refactor
Feb 23, 2020
Merged

Refactor information.py with lots of changes that makes the overall code cleaner and faster#741
sco1 merged 16 commits into
python-discord:masterfrom
Denayder:information-refactor

Conversation

@Denayder
Copy link
Copy Markdown

@Denayder Denayder commented Feb 6, 2020

Hello. I created #740 yesterday, but with lack of knowledge of forking and proper commit messages, the commit history became a slight bit messy. I'm here again with pretty much the same PR, with a few changes here and there. Thanks to @sco1 for the helpful advice so far.

A change log can be found here and in the commit messages:

  • No longer check if every role is @everyone or not. Simply skipping the first element in the .roles will skip the @everyone role.
  • Channel and status counting has been completely refactored. It now uses Counter(), and uses the __class__ attribute for channel counting.
    • I have updated the tests to reflect this.
  • No longer send a message every time a role conversion fails when using !role. Instead, append a list with all the failed roles, and send 1 message. This should be less spammy.
  • Imports were updated a slight bit. It didn't make much sense to me that separate elements from discord & typing was imported, and then the entire module was imported.
  • Users cannot have 0 roles, so there's no need to check if there is any.
  • Changed unnecessary if statement to an elif statement.
  • Made generally code look cleaner; there seemed to be a lot of unnecessary new lines and from my understanding, the general rule of thumb is that newlines should only be made whenever there's a reason to do so.

Some concern was brought up regarding the use of the __class__ attribute- I'm not sure if the use of Counter() fixes this concern or not.

Thank you once again for giving this PR a look. Let me know if there is anything you would like changed. :)

@Denayder Denayder requested a review from a team as a code owner February 6, 2020 21:29
@Denayder Denayder requested review from kosayoda and tagptroll1 and removed request for a team February 6, 2020 21:29
@Denayder Denayder requested review from sco1 and removed request for kosayoda and tagptroll1 February 6, 2020 21:37
@ikuyarihS ikuyarihS added a: information Related to information commands: (doc, help, information, reddit, site, tags) a: tests Related to tests (e.g. unit tests) p: 2 - normal Normal Priority status: needs review labels Feb 7, 2020
@ikuyarihS ikuyarihS requested review from a team and fiskenslakt and removed request for a team February 7, 2020 06:37
Comment thread bot/cogs/information.py Outdated
@MarkKoz MarkKoz added the t: feature New feature or request label Feb 9, 2020
Comment thread bot/cogs/information.py Outdated
Comment thread bot/cogs/information.py Outdated
Comment thread bot/cogs/information.py Outdated
Comment thread bot/cogs/information.py Outdated
@MarkKoz MarkKoz added s: waiting for author Waiting for author to address a review or respond to a comment and removed status: needs review labels Feb 9, 2020
@Denayder
Copy link
Copy Markdown
Author

All the requested changes should have been made, although, I can't seem to figure out why the test is failing, even though I did update it to reflect the changes made. Help on this would be appreciated, since I have zero clue 😓

@MarkKoz
Copy link
Copy Markdown
Contributor

MarkKoz commented Feb 14, 2020

All the requested changes should have been made, although, I can't seem to figure out why the test is failing, even though I did update it to reflect the changes made. Help on this would be appreciated, since I have zero clue 😓

It's due to dedent() not removing the leading whitespace. It only removes it if it is common to all lines. The lines added by channel_counts lack the leading whitespace. Therefore, the whitespace is not common to all lines and it isn't remove for any of the lines.

@sco1
Copy link
Copy Markdown
Contributor

sco1 commented Feb 15, 2020

To expand on the above, this is what comes out of the dedent call with the pre-formatted channel list:

                **Server information**
                Created: 1 year, 11 months and 20 days ago
                Voice region: us-east
                Features: 

                **Counts**
                Members: 8
                Roles: 17
                Category channels: 12
Text channels: 69
Voice channels: 11

                **Members**
                <:status_online:470326272351010816> 1
                <:status_idle:470326266625785866> 1
                <:status_dnd:470326272082313216> 0
                <:status_offline:470326266537705472> 6

Which obviously doesn't match what we have in the unit test, though Discord renders it fine because they (usually) strip the leading whitespace.

One approach that can be used is to insert the channel list after dedent is done, using a Template string, which should fix the formatting issue:

# Because channel_counts lacks leading whitespace, it breaks the dedent if it's inserted directly by the
# f-string. While this is correctly formated by Discord, it makes unit testing difficult. To keep the formatting
# without joining a tuple of strings we can use a Template string to insert the already-formatted channel_counts
# after the dedent is made.
embed_description = Template(
    textwrap.dedent(f"""
        **Server information**
        Created: {created}
        Voice region: {region}
        Features: {features}

        **Counts**
        Members: {member_count:,}
        Roles: {roles}
        $channel_counts

        **Members**
        {constants.Emojis.status_online} {statuses[Status.online]:,}
        {constants.Emojis.status_idle} {statuses[Status.idle]:,}
        {constants.Emojis.status_dnd} {statuses[Status.dnd]:,}
        {constants.Emojis.status_offline} {statuses[Status.offline]:,}
    """)
).substitute({"channel_counts": channel_counts})

embed = Embed(colour=Colour.blurple(), description=embed_description)

Alternatively, you could also use string concatenation, with our without a join:

embed = Embed(
    colour=Colour.blurple(),
    description=(
        "**Server information**\n"
        f"Created: {created}\n"
        f"Voice region: {region}\n"
        f"Features: {features}\n\n"
        "**Counts**\n"
        f"Members: {member_count:,}\n"
        f"Roles: {roles}\n"
        f"{channel_counts}\n\n"
        "**Members**\n"
        f"{constants.Emojis.status_online} {statuses[Status.online]:,}\n"
        f"{constants.Emojis.status_idle} {statuses[Status.idle]:,}\n"
        f"{constants.Emojis.status_dnd} {statuses[Status.dnd]:,}\n"
        f"{constants.Emojis.status_offline} {statuses[Status.offline]:,}\n"
    )
)
embed = Embed(
    colour=Colour.blurple(),
    description="\n".join(
        (
            "**Server information**",
            f"Created: {created}",
            f"Voice region: {region}",
            f"Features: {features}",
            "\n**Counts**",
            f"Members: {member_count:,}",
            f"Roles: {roles}",
            f"{channel_counts}",
            "\n**Members**",
            f"{constants.Emojis.status_online} {statuses[Status.online]:,}",
            f"{constants.Emojis.status_idle} {statuses[Status.idle]:,}",
            f"{constants.Emojis.status_dnd} {statuses[Status.dnd]:,}",
            f"{constants.Emojis.status_offline} {statuses[Status.offline]:,}",
        )
    )
)

The first approach probably makes the most sense.

@Denayder
Copy link
Copy Markdown
Author

Apologies for the delay, and thanks for the assistance @sco1. All changes have been made and checks have been fixed. This should be ready for another review 😁

Copy link
Copy Markdown
Contributor

@MarkKoz MarkKoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work. Thanks!

@MarkKoz MarkKoz added status: needs review and removed s: waiting for author Waiting for author to address a review or respond to a comment labels Feb 21, 2020
@sco1 sco1 merged commit b2edc85 into python-discord:master Feb 23, 2020
@Denayder Denayder deleted the information-refactor branch February 23, 2020 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: information Related to information commands: (doc, help, information, reddit, site, tags) a: tests Related to tests (e.g. unit tests) p: 2 - normal Normal Priority t: feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants